#!/usr/bin/env python
# coding: utf-8

# In[8]:


from ete3 import Tree
t = Tree( "((a,b),c);" )
t.show()


# In[4]:


from ete3 import Tree
t = Tree( "((a,b),c);" )
t.render("mytree.png", w=183, units="mm")
t.show()


# In[20]:


from ete3 import Tree, TreeStyle
t = Tree()
t.populate(10, random_branches =True)
ts = TreeStyle()
ts.show_leaf_name = True
ts.show_branch_length = True
ts.show_branch_support = True
t.show(tree_style=ts)


# In[19]:


from ete3 import Tree, TreeStyle
t = Tree()
t.populate(10, random_branches =True)
ts = TreeStyle()
ts.show_leaf_name = True
ts.scale = 120
t.show(tree_style=ts)


# In[21]:


from ete3 import Tree, TreeStyle
t = Tree()
t.populate(10, random_branches =True)
ts = TreeStyle()
ts.show_leaf_name = True
ts.branch_vertical_margin = 10
t.show(tree_style=ts)


# In[22]:


from ete3 import Tree, TreeStyle
t = Tree()
t.populate(10, random_branches =True)
ts = TreeStyle()
ts.show_leaf_name = True
ts.rotation = 90
t.show(tree_style=ts)


# In[23]:


from ete3 import Tree, TreeStyle
t = Tree()
t.populate(30)
ts = TreeStyle()
ts.show_leaf_name = True
ts.mode = "c"
ts.arc_start = -100
ts.arc_span = 100
t.show(tree_style=ts)


# In[31]:


from ete3 import Tree, TreeStyle, TextFace
t= Tree( "((a,b),c);" )
ts = TreeStyle()
ts.show_leaf_name = True
ts.title.add_face(TextFace("ETE Tree", fsize=20), column=0)
t.show(tree_style=ts)


# In[6]:


from ete3 import Tree, faces, AttrFace, TreeStyle, NodeStyle

def layout(node):
    if node.is_leaf():
        N = AttrFace("name", fsize=30)
        faces.add_face_to_node(N, node, 0, position="aligned")

def get_example_tree():

    # Set dashed blue lines in all leaves
    nst1 = NodeStyle()
    nst1["bgcolor"] = "LightSteelBlue"
    nst2 = NodeStyle()
    nst2["bgcolor"] = "Moccasin"
    nst3 = NodeStyle()
    nst3["bgcolor"] = "DarkSeaGreen"
    nst4 = NodeStyle()
    nst4["bgcolor"] = "Khaki"


    t = Tree("((((A,B),C), ((D,E),(F,G))), ((H,I),K));")
    for n in t.traverse():
        n.dist = 0

    n1 = t.get_common_ancestor("A", "B", "C")
    n1.set_style(nst1)
    n2 = t.get_common_ancestor("D", "E", "F", "G")
    n2.set_style(nst2)
    n3 = t.get_common_ancestor("H", "I", "K")
    n3.set_style(nst3)
    n4 = t.get_common_ancestor("F", "G")
    n4.set_style(nst4)
    ts = TreeStyle()
    ts.layout_fn = layout
    ts.show_leaf_name = False

    ts.mode = "c"
    ts.root_opening_factor = 1
    return t, ts

if __name__ == "__main__":
    t, ts = get_example_tree()
    #t.render("node_background.png", w=400, tree_style=ts)
    t.show(tree_style=ts)


# In[6]:


from ete3 import Tree, TreeStyle, TextFace
t = Tree('Downloads/my_tree.fasta.final_tree.nw')
ts = TreeStyle()
ts.show_leaf_name = True
ts.show_branch_length = True
ts.show_branch_support = True
ts.title.add_face(TextFace("my-tree", fsize=40), column=0)
t.show(tree_style=ts)


# In[3]:


from ete3 import Tree, TreeStyle, TextFace

t = Tree('Downloads/my_tree.fasta.final_tree.nw')
#t.populate(50)
ts = TreeStyle()
ts.mode = "c"
ts.show_leaf_name = True
ts.arc_start = -360 # 0 degrees = 3 o'clock
ts.arc_span = 360
t.show(tree_style=ts)


# In[3]:


from ete3 import Tree
#t=Tree( "((HAA:0.3,IAA:0.1):0.5,A:1,(BAA:0.4,(CAA:0.5,(JAA:1.3,(FAA:1.2,DAA:0.1):0.5):0.5):0.5):0.5);" )
t = Tree('Downloads/Project_master_thesis/output_tree/clustalo_default-trimal01-none-raxml_default_bootstrap/All_HA_march2020_cls95.fasta.final_tree.nw')
#print (t)
h=6.7 # the height value at which the tree will be trimmed 

first_node = t.get_tree_root()

all_final_leaf=[]
for nod in t.traverse('preorder'):
    if len(nod.name) > 0:
        all_final_leaf.append(nod.name)


key_nodes=[]

for nod in t.traverse('preorder'):
    distance=t.get_distance(nod, first_node)
    if distance > h and not True in [True for x in nod.get_ancestors() if [True for wazne in key_nodes if x ==wazne]]:
        key_nodes.append(nod)
        
key_nodes_names=[]
for nod in key_nodes:
    if nod.is_leaf():
        key_nodes_names.append([str(nod.name)])
    else:
        #key_nodes_names.append(",".join([str(x.name) for x in nod.get_leaves()]))
        key_nodes_names.append([str(x.name) for x in nod.get_leaves()])
        
        
flatten_list=[y for x in key_nodes_names for y in x if ',' not in y]

for leaf in all_final_leaf:
    if leaf not in flatten_list:
        key_nodes_names.append([leaf])

print(len(key_nodes_names))
with open('some_silly_name_67_2.csv', 'w') as f:
    for element in key_nodes_names:
        f.write(",".join(map(str, element))+"\n")


# In[6]:


get_ipython().run_line_magic('pwd', '')


# In[5]:


ala='>gb:CY066943|ncbiId:ADK33681.1|UniProtKB:D8KV33|Organism:Influenza A virus (A/Aarhus/INS237/2009(H1N1))|Strain '


# In[6]:


print(ala)


# In[9]:


ala.split('(')[2].split(')')[0]


# In[ ]:




